<template>
    <el-form :model="form"
             :rules="rules"
             ref="formRef">

        <el-form-item label="海报名称"
                      prop="title"
                      :label-width="formLabelWidth">
            <el-input v-model="form.title"></el-input>
        </el-form-item>

        <el-form-item label="上传图片"
                      prop="url"
                      :label-width="formLabelWidth">
            <el-upload class="avatar-uploader"
                       action
                       :http-request="uploadFile"
                       :show-file-list="false">
                <img v-if="form.url"
                     :src="form.url"
                     class="avatar" />
                <i v-else
                   style="line-height: 100px;"
                   class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth">
            <el-button plain
                       @click="resetForm('formRef')">取消</el-button>
            <el-button type="primary"
                       @click="submitForm('formRef')">确定</el-button>
        </el-form-item>
    </el-form>
</template>

<script>
export default {
    data() {
        return {

            form: {
                url: "",
                title: ""
            },
            formLabelWidth: "120px",
            rules: {
                title: [{ required: true, message: "请输入商品标题", trigger: "blur" }],
                url: [
                    { required: true, message: "请输入商品类别", trigger: "blur" }
                ]
            }
        };
    },
    methods: {
        uploadFile(file) {
            this.$service.uploadFileFunction(file, result => {
                this.form.url = result.url;
            });
        },
        submitForm(formName) {
            this.$refs[formName].validate(valid => {
                if (valid) {
                    this.$service.addPoster(this.form)
                        .then(res => {
                            if (res.code = '00000')
                                this.$emit('submit')
                        })
                }
            });
        },
        resetForm(formName) {
            this.$emit("categorySubmit", null);
            this.$refs[formName].resetFields();
        },
    }
};
</script>

<style lang="scss">
</style>